package com.xxxx.rpc.service;

import com.xxxx.api.service.IRegisterService;
import com.xxxx.api.pojo.Admin;
import com.xxxx.api.pojo.RegisterParams;
import com.xxxx.api.pojo.ResultInfo;
import com.xxxx.api.utils.AssertUtil;
import com.xxxx.api.utils.Md5Util;
import com.xxxx.api.utils.PhoneUtil;
import com.xxxx.api.utils.PwdUtil;
import com.xxxx.rpc.mapper.AdminMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;

/**
 * @author nathan-wang on 2020-10-31
 */
@Service(version = "1.0")
public class RegisterServiceImpl implements IRegisterService {
    //注入mapper
    @Resource
    private AdminMapper adminMapper;

    /**
     * 用户注册业务逻辑:
     * 1.用户名存在与否校验&手机号码存在与否校验
     * 2.4个必填字段非空校验:phone,userName,password,pwdConfirm
     * 3.电话合法性校验
     * 4.密码不与用户名一样
     * 5.密码合法性校验
     * 6.两次密码是否一致校验
     * 7.封装用户数据;默认值addTime
     * 8.调用adminMapper，insert并判断成功与否——密码使用Md5加密放入数据库
     * 9.返回注册成功的消息!
     *
     * @param registerParams
     * @return
     */
    public ResultInfo register(RegisterParams registerParams) {
        /*1.用户名存在与否校验;用户名查用户*/
        Admin admin =adminMapper.queryByUsername(registerParams.getUserName());
        AssertUtil.isTrue(null != admin, "用户名已存在，请重新设置！");
        /*1.电话号码是否存在，电话号码查用户*/
        Admin admin002=adminMapper.queryByPhone(registerParams.getPhone());
        AssertUtil.isTrue(null != admin002, "用户已存在，请重新设置！");
        /*2.4个必填字段非空校验*/
        AssertUtil.isTrue(StringUtils.isBlank(registerParams.getPhone()) ||
                StringUtils.isBlank(registerParams.getUserName()) ||
                StringUtils.isBlank(registerParams.getPassword()) ||
                StringUtils.isBlank(registerParams.getPwdConfirm()), "必填项不能为空！");
        /*3.电话合法性校验*/
        AssertUtil.isTrue(!PhoneUtil.isPhone(registerParams.getPhone()), "手机号码格式不正确!");
        /*4.密码不与用户名一样*/
        AssertUtil.isTrue(registerParams.getPassword().equals(registerParams.getUserName()), "密码不能与用户名一样！");
        /*5.密码合法性校验*/
        AssertUtil.isTrue(!PwdUtil.isValidPwd(registerParams.getPassword()), "密码格式不正确！");
        /*6.两次密码是否一致校验*/
        AssertUtil.isTrue(!registerParams.getPassword().equals(registerParams.getPwdConfirm()), "两次输入的密码不一致!");
        /*7.封装用户数据;默认值addTime;addTime为Integer类型!!!*/
//        Admin admin1 = new Admin();
        String userName = registerParams.getUserName();
        String phone = registerParams.getPhone();
        String password = Md5Util.encode(registerParams.getPassword());
        Integer addTime = Integer.parseInt(String.valueOf(System.currentTimeMillis() / 1000));
        /*8.调用adminMapper，insert并判断成功与否——密码使用Md5加密放入数据库*/
        AssertUtil.isTrue(adminMapper.addUser(
                userName,
                phone,
                password,
                addTime
        ) != 1, "sorry,注册失败！");
        /*9.返回注册成功的消息!*/
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCode(200);
        resultInfo.setMsg("注册成功！");
        return resultInfo;
    }

    /**
     * queryAdminById
     * @param adminId
     * @return
     */
    public Admin queryAdminById(short adminId){
       Admin admin =  adminMapper.queryByAdminId(adminId);
       return admin;
    }
}
